package org.example.web;

import org.example.util.Utils;
import org.example.vo.LoginProperties;
import org.example.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;

/**
 * packageName org.example.web
 *
 * @author jcspout
 * @version JDK 11
 * @className ForgetDo
 * @date 2024/6/13
 * @description TODO
 */
@RestController
@RequestMapping("forget")
public class ForgetDo {

    @Autowired
    LoginProperties loginProperties;

    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping("updatePwd")
    public Result updatePwd(@SessionAttribute(required = false) String vcode, @RequestParam("vcode") String svcode,
                            String password , String repwd, String eamil,
                            @SessionAttribute(required = false) String ename) {

//        Utils.check(vcode == null,"请先获取验证码");
//
//        Utils.check(Utils.isEmpty(password),"密码不能为空");
//
//        Utils.check(Utils.isEmpty(repwd),"再次输入密码不能为空");
//
//        Utils.check(!password.equals(repwd),"两次输入的密码不一致");
//
//        Utils.check(Utils.isEmpty(svcode),"请输入验证码");
//
//        Utils.check(!vcode.equals(svcode),"验证码错误");

        if(Utils.isEmpty(vcode)){
            return Result.fail("请先获取验证码");
        }else if(Utils.isEmpty(password)){
            return Result.fail("密码不能为空");
        }else if(Utils.isEmpty(repwd)){
            return Result.fail("再次输入密码不能为空");
        }else if(!password.equals(repwd)){
            return Result.fail("两次输入的密码不一致");
        }else if(Utils.isEmpty(svcode)){
            return Result.fail("请输入验证码");
        }else if(Utils.isEmpty(svcode)){
            return Result.fail("验证码错误");
        }



        String sql = "update %s set %s = ? where %s = ?";
        sql = String.format(sql, loginProperties.getUserTable(),
                loginProperties.getPwdColumn(),loginProperties.getEmail());

        if("md5".equalsIgnoreCase(loginProperties.getEncryption())){
            password = DigestUtils.md5DigestAsHex((ename + password).getBytes(StandardCharsets.UTF_8));
        }
        int update = jdbcTemplate.update(sql, password, eamil);

        if(update <= 0){
            return Result.fail("错误，请联系管理员");
        }

        return Result.ok("修改成功");
    }

    @RequestMapping("findEmail")
    public Result findEmail(String eamil, HttpSession session){

        if(eamil.isBlank()){
            return Result.fail("您还没有输入邮箱");
        }

        String sql = "select * from %s where %s = ?";
        sql = String.format(sql, loginProperties.getUserTable(),loginProperties.getEmail());

        try{
            Object user = jdbcTemplate.queryForObject(sql,
                    new BeanPropertyRowMapper<>(loginProperties.getUserClassObject()),
                    eamil);
            Field field = user.getClass().getDeclaredField("ename");
            field.setAccessible(true);
            Object ename = field.get(user);
            session.setAttribute("ename", String.valueOf(ename));
            return Result.ok("ok");
        }catch (EmptyResultDataAccessException e){
            return Result.fail("该邮箱还没有注册");
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

}